import React, {Component} from "react";
import {Button, Table} from "antd";

export default class SeedManager extends Component {
    seedColumns = [
        {
            title: 'ID',
            dataIndex: 'ID',
            key: 'ID',
        },
        {
            title: '创建时间',
            dataIndex: 'CreatedAt',
            key: 'CreatedAt',
        },
        {
            title: '名称',
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: 'Url',
            dataIndex: 'url',
            key: 'url',
        },
        {
            title: '解析方法名',
            dataIndex: 'function_name',
            key: 'function_name',
        },
        {
            title: '操作',
            render: (item) => (
                <Button type={"primary"}
                        onClick={() => this.bind(item)}
                >管理</Button>)
        },
    ];

    limit = 5;

    constructor(props) {
        super(props);

        this.state = {
            seeds: [],
            seedsCurrent: 0,
            seedsCount: 0,
            selectedSeed: null,
            bindVisible: false,

            nsqSettings: [],
        }
    };

    componentDidMount() {
        this._getSeeds(0)
    }

    bind(item) {

        this.setState({bindVisible: true, selectedSeed: item});
    }

    _getSeeds(offset) {
        window.$http.postForm("/seed/gets", {
            offset: offset,
            limit: this.limit,
        }).then(res => {
            if (res.status === 10000) {
                this.setState({seeds: res.seeds, seedsCount: res.count});
                return
            }
            window.$message.warn(res.message);
        }).catch(err => {
            window.$message.error(err.message);
        })
    }


    render() {
        return <>
            <Table rowKey={record => record.ID}
                   columns={this.seedColumns} dataSource={this.state.seeds}/>
        </>
    }
}